package cn.wanda.bi.sunshine.platform.server.service;

import cn.wanda.bi.sunshine.platform.server.aspect.Logging;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.LabelManagementFilterDetailMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.LabelManagementFilterMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.UserLabelAuthMenuMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.*;
import cn.wanda.bi.sunshine.platform.server.controller.request.LabelManagementFilterRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangfupeng1
 */
@Service
public class LabelManagementFilterService implements Logging {

    public static final Logger logger = LoggerFactory.getLogger(LabelManagementFilterService.class);


    @Autowired
    private LabelManagementFilterMapper labelManagementFilterMapper;
    @Autowired
    private LabelManagementFilterDetailMapper  labelManagementFilterDetailMapper;

    //增加
    public SimpleResponse<LabelManagementFilterRequest> add(LabelManagementFilterRequest labelManagementFilterRequest) {
        labelManagementFilterMapper.insertSelective(labelManagementFilterRequest.getLabelManagementFilter());
        List<LabelManagementFilterDetail> labelManagementFilterDetails = labelManagementFilterRequest.getLabelManagementFilterDetail();
        for (LabelManagementFilterDetail labelManagementFilterDetail:labelManagementFilterDetails
             ) {
            labelManagementFilterDetail.setParentFilterId(labelManagementFilterRequest.getLabelManagementFilter().getId());
            labelManagementFilterDetailMapper.insertSelective(labelManagementFilterDetail);
        }
        return SimpleResponse.success(labelManagementFilterRequest);
    }

    //根据PageId进行查询
    public SimpleResponse<LabelManagementFilterRequest> queryById(Integer id) {
        LabelManagementFilter labelManagementFilter = labelManagementFilterMapper.selectByPrimaryKey(id);
        LabelManagementFilterDetailExample labelManagementFilterDetailExample = new LabelManagementFilterDetailExample();
        labelManagementFilterDetailExample.createCriteria().andParentFilterIdEqualTo(labelManagementFilter.getId());
        List<LabelManagementFilterDetail> labelManagementFilterDetails = labelManagementFilterDetailMapper.selectByExample(labelManagementFilterDetailExample);
        LabelManagementFilterRequest labelManagementFilterRequest = new LabelManagementFilterRequest();
        labelManagementFilterRequest.setLabelManagementFilter(labelManagementFilter);
        labelManagementFilterRequest.setLabelManagementFilterDetail(labelManagementFilterDetails);
        return SimpleResponse.success(labelManagementFilterRequest);
    }

	//根据page id进行修改
	@CacheEvict(value = "cache:userLabelAuthMenu", key = "'cache:userLabelAuthMenu:'+#userLabelAuthMenu.getId()")
	public SimpleResponse<LabelManagementFilterRequest> update(LabelManagementFilterRequest labelManagementFilterRequest) {

        labelManagementFilterMapper.updateByPrimaryKeySelective(labelManagementFilterRequest.getLabelManagementFilter());
        LabelManagementFilterDetailExample labelManagementFilterDetailExample = new LabelManagementFilterDetailExample();
        labelManagementFilterDetailExample.createCriteria().andParentFilterIdEqualTo(labelManagementFilterRequest.getLabelManagementFilter().getId());
        labelManagementFilterDetailMapper.deleteByExample(labelManagementFilterDetailExample);
        List<LabelManagementFilterDetail> labelManagementFilterDetails = labelManagementFilterRequest.getLabelManagementFilterDetail();
        for (LabelManagementFilterDetail labelManagementFilterDetail:labelManagementFilterDetails
                ) {
            labelManagementFilterDetail.setParentFilterId(labelManagementFilterRequest.getLabelManagementFilter().getId());
            labelManagementFilterDetailMapper.insertSelective(labelManagementFilterDetail);
        }
        return SimpleResponse.success(labelManagementFilterRequest);
    }
    //删除page
	@CacheEvict(value = "cache:userLabelAuthMenu", key = "'cache:userLabelAuthMenu:'+#userLabelAuthMenu.getId()")
    public SimpleResponse delete( Integer id) {
        labelManagementFilterMapper.deleteByPrimaryKey(id);
        LabelManagementFilterDetailExample labelManagementFilterDetailExample = new LabelManagementFilterDetailExample();
        labelManagementFilterDetailExample.createCriteria().andParentFilterIdEqualTo(id);
        labelManagementFilterDetailMapper.deleteByExample(labelManagementFilterDetailExample);
        return SimpleResponse.success();
    }
    //查询所有Page
    public ListResponse<LabelManagementFilter> list(Integer index, Integer limit) {
        LabelManagementFilterExample labelManagementFilterExample = new LabelManagementFilterExample();
        labelManagementFilterExample.setOffset(index * limit - limit);
        labelManagementFilterExample.setLimit(limit);
        List<LabelManagementFilter> labelManagementFilters = labelManagementFilterMapper.selectByExample(labelManagementFilterExample);

        int count = labelManagementFilterMapper.countByExample(labelManagementFilterExample);
        return ListResponse.success(labelManagementFilters,count,index,limit);
    }


}
